package color.koitq.picker.activty

import android.content.ContentValues
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import color.koitq.picker.R
import color.koitq.picker.ad.AdActivity
import color.koitq.picker.adapter.HomeColorAdapter
import color.koitq.picker.decoration.GridSpaceItemDecoration
import color.koitq.picker.entity.PaletteModel
import color.koitq.picker.entity.RefreshEvent
import color.koitq.picker.entity.Tab3Model
import color.koitq.picker.entity.Tab4Model
import color.koitq.picker.util.SQLdm
import color.koitq.picker.util.ThisUtils
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import kotlinx.android.synthetic.main.item_home_color_list_activity.*
import kotlinx.android.synthetic.main.item_home_color_list_activity.view.*
import org.greenrobot.eventbus.EventBus
import org.litepal.LitePal
import org.litepal.extension.find
import java.util.*

class HomeColorListActivity : AdActivity() {
    private lateinit var mAdapter: HomeColorAdapter
    override fun getContentViewId(): Int {
        return R.layout.item_home_color_list_activity
    }

    override fun init() {
        topbar.addLeftBackImageButton().setOnClickListener { finish() }
        initList()
        val id = intent.getIntExtra("id", 0)
        val model = intent.getParcelableExtra<Tab3Model>("model")
        val tab4Model = intent.getParcelableExtra<Tab4Model>("tab4Model")
        if (model != null) {
            //tab3跳转
            loadTab2Data(model)
        } else if (id != 0) {
            //tab1跳转
            val id = intent.getIntExtra("id", 0)
            loadTab1Data(id)

        } else if (tab4Model != null) {
            //预留跳转
            when (tab4Model.modelFlag) {
                ThisUtils.PALETTERMODEL -> {
                    loadTab1Data(tab4Model.modelId)
                }
                ThisUtils.TAB3MODEL -> {

                    loadTab2Data(
                        LitePal.where("id=?", tab4Model.modelId.toString())
                            .find(Tab3Model::class.java)[0]
                    )
                }
                ThisUtils.NOMODEL -> {
                    topbar.setTitle("配色详情")
                    collection.visibility = View.GONE
                    mAdapter.setNewInstance(tab4Model.colors)
                }

            }
        }

        showSecondPageAd_TwoBanner(bannerView, bannerView2)

    }

    private fun loadTab2Data(model: Tab3Model) {
        topbar.setTitle("配色详情")
        initListFromTab3(model)
        collection.setImageResource(if (model.collection == 1) R.mipmap.ic_collection_1 else R.mipmap.ic_collection_0)
        collection.setOnClickListener {
            var cv = ContentValues()
            cv.put("collection", if (model.collection == 0) 1 else 0)
            LitePal.update(Tab3Model::class.java, cv, model.id)
            model.collection = if (model.collection == 0) 1 else 0
            collection.setImageResource(if (model.collection == 1) R.mipmap.ic_collection_1 else R.mipmap.ic_collection_0)
            ToastUtils.showShort(if (model.collection == 1) "收集成功" else "取消收集")
            EventBus.getDefault().post(RefreshEvent())
        }

    }

    private fun loadTab1Data(id: Int) {
        val paletteModels = SQLdm.queryColor(id)
        val paletteModel = paletteModels[0]
        topbar.setTitle(paletteModel.title)
        initListFromHome(paletteModel)
        collection.setImageResource(if (paletteModel.isFavorite == 1) R.mipmap.ic_collection_1 else R.mipmap.ic_collection_0)
        collection.setOnClickListener {
            paletteModel.isFavorite = if (paletteModel.isFavorite == 0) 1 else 0
            collection.setImageResource(if (paletteModel.isFavorite == 1) R.mipmap.ic_collection_1 else R.mipmap.ic_collection_0)
            ToastUtils.showShort(if (paletteModel.isFavorite == 1) "收集成功" else "取消收集")
            SQLdm.setFavorite(paletteModel.isFavorite, paletteModel.paletteId)
            EventBus.getDefault().post(RefreshEvent())
        }
    }

    private fun initListFromHome(paletteModel: PaletteModel?) {
        var colors = paletteModel?.colors
        if (!colors.isNullOrEmpty() && colors.contains(",")) {
            val stringList = ArrayList<String>()
            colors.split(",").forEach {
                stringList.add(it)
            }
            mAdapter.setNewInstance(stringList.toMutableList())
        }
    }

    private fun initListFromTab3(model: Tab3Model) {
        var list = mutableListOf<String>(
            model.color1,
            model.color2,
            model.color3,
            model.color4,
            model.color5
        )
        mAdapter.setNewInstance(list.toMutableList())
    }

    private fun initList() {
        list.layoutManager = GridLayoutManager(activity, 1)
        list.addItemDecoration(
            GridSpaceItemDecoration(
                1, QMUIDisplayHelper.dp2px(activity, 20),
                QMUIDisplayHelper.dp2px(activity, 0)
            )
        )
        mAdapter = HomeColorAdapter()
        mAdapter.listener(object : HomeColorAdapter.HomeItemColorClickListener {
            override fun click(s: String?) {
                ClipboardUtils.copyText(s)
                ToastUtils.showShort("已复制到剪贴板")
            }
        })
        list.adapter = mAdapter

    }
}